package com.bckj.fastboot.core.sql.meta;

import lombok.Getter;

import java.util.Objects;

@Getter
public class Column implements Expression {
    private final TableLike table;
    private final String column;
    private String alias;

    public Column(TableLike table, String column, String alias) {
        this.table = table;
        this.column = column;
        this.alias = alias;
    }

    public Column(TableLike table, String column) {
        this.table = table;
        this.column = column;
    }

    public String getTableColumnName() {
        return table.getAlias() + "." + column;
    }


    public Column alias(String alias) {
        this.alias = alias;
        return this;
    }

    @Override
    public int hashCode() {
        return Objects.hash(column);
    }

    @Override
    public String toString() {
        if (alias == null) {
            return getTableColumnName();
        }
        return getTableColumnName() + " AS " + alias;
    }
}
